<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>库存预警</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>

    <style>
        .el-tree {
            width: 100px;
            display: block;
            position: absolute;
            left: 0px;
            top: 100px;
        }

        #table {
            position: absolute;
            left: 110px;
            top: 100px;
        }

        .el-input {
            width: 150px;
        }
        #search{
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            border-style: solid;
            border-width: 1px;
            border-color: #ece4ff;
        }
    </style>
</head>
<body>
<div id="bigger">
    <div id="search">
        仓库名称:
        <el-select v-model="tMenuNo" placeholder="请选择仓库">
            <el-option label="默认" :value="0"></el-option>
            <el-option v-for="tm in tMenuTree" :label="tm.wareName" :value="tm.wareNo"></el-option>
        </el-select>
        产品名称:
        <el-input v-model="tProductName" placeholder="请输入内容"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="butSearch">搜索</el-button>
    </div>

    <!--    树形菜单-->
    <el-tree :data="tMenuTree" :props="defaultProps" width="180px" accordion @node-click="handleNodeClick"></el-tree>

    <el-dialog title="设置预警值" :visible.sync="dialogFormVisible">
        仓库名称:
        <span style="margin-left: 10px">{{ warning.wareName }}</span><br>
        产品名称:
        <span style="margin-left: 10px">{{ warning.pName }}</span><br>
        当前预警值:
        <span style="margin-left: 10px">{{ warning.warningOldNum }}</span><br>

        <el-form :model="warning">
            <el-form-item label="设置预警值" label-width="80px">
                <el-input v-model="warning.warningNum" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateWarning">确 定</el-button>
        </div>
    </el-dialog>
    <div id="table">
        <el-table
                :data="listTWarning"
                tooltip-effect="dark"
                ref="selectOrgan"
                style="width: 100%"
        >
            <!--        @select="selectCheck"-->
            <el-table-column
                    label="编号"
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    label="仓库名称"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.tmenu.wareName }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="产品编号"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.tproduct.pnumber }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="产品名称"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.tproduct.pname }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="库存量"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.tproduct.availableNum }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="状态"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px;color: red">库存不足</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="备注"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.creatTime }}</span>
                </template>
            </el-table-column>

            <el-table-column
                    label="当前产品预警值"
                    width="100">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.warningNum }}</span>
                </template>
            </el-table-column>


            <el-table-column label="设置预警值">
                <template slot-scope="scope">
                    <el-button
                            size="small"
                            @click="handleEdit(scope.$index, scope.row)">预警值
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</div>

<script>
    new Vue({
        el: '#bigger',
        data: {
            // 树形菜单顶级
            tMenuTree: null,
            defaultProps: {
                children: 'listTReservoirCLs',
                label: 'wareName'
            },

            listTWarning: null,

            tMenuNo: "",
            tProductId: "",
            tProductName: "",

            warning:{productId:0,warningNum:0,warehouseId:0,wareName:'',pName:'',warningOldNum:0},
            dialogFormVisible:false,
        },
        methods: {
            findTMenuTree: function () {
                axios.get('/tMenu/tree').then(res => {
                    this.tMenuTree = res.data;
                    this.tMenuTree.forEach(tMenu => {
                        tMenu.listTReservoirCLs.forEach(reser => {
                            reser['wareName'] = reser.goodsName;
                            reser['listTReservoirCLs'] = reser.listTShelvesCls;
                            reser.listTShelvesCls.forEach(she => {
                                she['wareName'] = she.mainProduct;
                            })
                        })
                    })
                })
            },
            findTWarning: function () {
                if (this.tMenuNo=="0"){
                    this.tMenuNo="";
                }
                axios.get('/tWarning?tMenuNo=' + this.tMenuNo + "&&tProductId=" + this.tProductId + "&&tProductName=" + this.tProductName).then(res => {
                    this.listTWarning = res.data;
                })
            },
            handleNodeClick: function (data, index) {
                if (index.level == 1) {
                    this.tMenuNo = data.id;
                    this.findTWarning();
                }
            },
            butSearch: function () {
                this.findTWarning();
            },
            handleEdit:function (index,row) {
                this.warning.productId=row.productId;
                this.warning.warningOldNum=row.warningNum;
                this.warning.warehouseId=row.warehouseId;
                this.warning.wareName=row.tmenu.wareName;
                this.warning.pName=row.tproduct.pname;
                this.dialogFormVisible=true;
            },
            updateWarning:function () {
                axios.put('/tWarning',this.warning).then(res=>{
                    if (res.data=='ok'){
                        this.dialogFormVisible=false;
                        this.$message('更新成功');
                        this.findTWarning();
                    }
                })
            }
        },
        mounted() {
            this.findTMenuTree();
        }
    })
</script>
</body>
</html>